home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10396 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: nntp.coast.net!torn!news!apollo!saed
  3. From: saed@engn.uwindsor.ca (Saed Aryan,13325,1100,g)
  4. Subject: [Q] how declare a Template class as a friend 
  5. X-Nntp-Posting-Host: apollo.engn.uwindsor.ca
  6. Message-ID: <Dnx660.1Ex@news.uwindsor.ca>
  7. Keywords: template, friend
  8. Sender: news@news.uwindsor.ca (Usenet)
  9. Reply-To: saed@engn.uwindsor.ca
  10. Organization: VLSI Research Group - University of Windsor
  11. Date: Thu, 7 Mar 1996 22:51:36 GMT
  12.  
  13. Hi all,
  14.  
  15. --- How do you declare a template class to be a friend of
  16.     another non-template class?
  17.  
  18. template <class T> class A {};    // the template class A<T's>
  19. class B { friend class A };    // won't do it, A is not a class
  20. template <class V> class B { friend class A<V> };    // B<V's> are not wanted
  21.  
  22. The point is to declare all classes A<whatever> to be friends of class B.
  23. My aim is to let all classes A<whatever> access private member data of class B,
  24. while B may not be a parent of all classes A<whatever>.
  25.  
  26. Of course I can go about this by declaring a function f() to be a friend of B,
  27. and have the methods of A<whatever> access B's private member data through f().
  28. But I don't consider this good encapsulation, since any client may use f().
  29.  
  30. As a remedy to the bad encapsulation, one could also declare a method of A,
  31. say g(), to be a friend of B. But consequently one would ask how to declare all
  32. methods A<whatever>::g() to be friends of B. Friendship is namely declared
  33. inside B, not in the A's.
  34.  
  35. If a parent class P of all the A<whatever> classes were to be a friend of B,
  36. then regretfully the kids, the derived classes A<whatever>, would not inherit the
  37. friendship. Sigh.
  38.  
  39. Any suggestions?
  40.  
  41. Thanks in advance, and please send a copy of your posting to my e-mail as well.
  42. Aryan.             saed@engn.uwindsor.ca
  43.  
  44.  
  45.